package net.maku.wx.service.impl;

import lombok.AllArgsConstructor;
import net.maku.framework.security.user.UserDetail;
import net.maku.system.enums.UserStatusEnum;
import net.maku.wx.convert.WxUserConvert;
import net.maku.wx.dao.WxUserDao;
import net.maku.wx.entity.WxUserEntity;
import net.maku.wx.service.WxUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

/**
 * 用户 UserDetails 信息
 *
 * @author 阿沐 babamu@126.com
 */
@Service
@AllArgsConstructor
public class WxUserDetailsServiceImpl implements WxUserDetailsService {

    private final WxUserDao wxUserDao;

    @Override
    public UserDetails getUserDetails(WxUserEntity userEntity) {
        // TODO 如果不存在，则新增小程序用户

        // 转换成UserDetail对象
        UserDetail userDetail = WxUserConvert.INSTANCE.convertDetail(userEntity);

        // 账号不可用
        if (userEntity.getStatus() == UserStatusEnum.DISABLE.getValue()) {
            userDetail.setEnabled(false);
        }
        return userDetail;
    }
}
